package com.jiezheng.huinong.action;

import java.util.Map;

import com.jiezheng.huinong.dao.PeopleManageDAOImpl;
import com.jiezheng.huinong.model.OperationStatus;
import com.jiezheng.huinong.model.PeopleInfo;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class PeopleManageAction extends ActionSupport {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7410121968714385760L;

	// 所有的居民信息
	private PeopleInfo people = new PeopleInfo();

	private String idCardList = "";
	
	/**
	 * 增加居民信息
	 * 
	 * @return
	 */
	public String addPeople() {
		// 增加用户部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");
		people.setDeptNo(deptNo);
		people.setStatus(OperationStatus.TO_AUDIT);
		
		(new PeopleManageDAOImpl()).addPeople(people);
		return SUCCESS;
	}

	/**
	 * 删除居民信息
	 * 
	 * @return
	 */
	public String deletePeople() {
		(new PeopleManageDAOImpl()).deletePeople(people.getIdCard());
		return SUCCESS;
	}

	/**
	 * 修改居民信息表单
	 */
	public String preModifyPeople() {
		people = (new PeopleManageDAOImpl()).getPeopleInfo(people.getIdCard());
		return SUCCESS;
	}

	/**
	 * 修改居民信息
	 * 
	 * @return
	 */
	public String modifyPeople() {
		people.setStatus(OperationStatus.TO_AUDIT);
		(new PeopleManageDAOImpl()).modifyPeople(people);
		return SUCCESS;
	}

	/**
	 * 送审居民信息
	 * 
	 * @return
	 */
	public String toAuditPeople() {
		audit(OperationStatus.NO_AUDIT);
		return SUCCESS;
	}

	/**
	 * 审核通过居民信息
	 * 
	 * @return
	 */
	public String approveAudit() {
		audit(OperationStatus.AUDITED);
		return SUCCESS;
	}
	
	/**
	 * 审核全部通过居民信息
	 * 
	 * @return
	 */
	public String approveAllAudit() {
		// 增加用户部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");
		people.setDeptNo(deptNo);
		people.setStatus(OperationStatus.AUDITED);
		(new PeopleManageDAOImpl()).approveAll(people);
		return SUCCESS;
	}

	/**
	 * 审核不通过居民信息
	 * 
	 * @return
	 */
	public String rejectAudit() {
		audit(OperationStatus.REJECTED);
		return SUCCESS;
	}

	public void audit(Integer status) {
		String[] idCards = idCardList.split("[:]");
		for (String idCard : idCards) {
			if (!idCard.isEmpty()) {
				PeopleInfo people = new PeopleInfo();
				people.setIdCard(idCard);
				people.setStatus(status);
				(new PeopleManageDAOImpl()).changeStatus(people);
			}
		}
	}

	public PeopleInfo getPeople() {
		return people;
	}

	public void setPeople(PeopleInfo people) {
		this.people = people;
	}

	public String getIdCardList() {
		return idCardList;
	}

	public void setIdCardList(String idCardList) {
		this.idCardList = idCardList;
	}
}
